package com.gosophia.reportData.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.reportData.ws.ReportJobWebService;
import com.gosophia.reportData.ws.impl.ReportJobWebServiceImpl;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * 动态表单上报
 * 
 * @author zhao_yanfang
 * 
 */
@Component
@Scope("prototype")
public class FlowFormReportAction extends BaseSupportAction {

	private static final Log logger = LogFactory
			.getLog(ReportJobWebServiceImpl.class);
	private Map<String, Object> parame;
	private Long flowFormId;// 表单编号
	private Long jobId;
	private Long launchId;
	private String reportDescription;
	@Autowired
	private ReportJobWebService reportJobWebService;
	
	private String fromFlag;

	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		Map<Long, Object> data = mapConverter(parame);
		logger.info("电子表单上报:" + data);
		reportJobWebService.flowFormSubmitted(data, jobId, launchId,
				userInfoId, flowFormId, reportDescription);
		super.getRequestResult().setMessage("电子表单上报成功");
		super.getRequestResult().setRsltCode("0");
		ActionContext.getContext().getSession().put("_lastOpResult",
				super.getRequestResult());
		return Action.SUCCESS;
	}

	public Map<String, Object> getParame() {
		return parame;
	}

	public void setParame(Map<String, Object> parame) {
		this.parame = parame;
	}

	/**
	 * 将页面传过来的数据进行类型转换
	 * 
	 * @param source
	 * @return
	 */
	public Map<Long, Object> mapConverter(Map<String, Object> source) {
		Map<Long, Object> data = null;
		if (source != null) {
			data = new HashMap<Long, Object>();
			for (Entry<String, Object> entry : source.entrySet()) {
				Long key = Long.parseLong(entry.getKey());
				String[] values = (String[]) entry.getValue();
				Object value = values[0];
				data.put(key, value);
			}
		}
		return data;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getLaunchId() {
		return launchId;
	}

	public void setLaunchId(Long launchId) {
		this.launchId = launchId;
	}

	public Long getFlowFormId() {
		return flowFormId;
	}

	public void setFlowFormId(Long flowFormId) {
		this.flowFormId = flowFormId;
	}

	public String getReportDescription() {
		return reportDescription;
	}

	public void setReportDescription(String reportDescription) {
		this.reportDescription = reportDescription;
	}

    public String getFromFlag() {
        return fromFlag;
    }

    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }
	
	

}